package com.prodavalnik.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.prodavalnik.model.Category;
import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.Product;

/**
 * Servlet implementation class CategorySearchServlet
 */
@WebServlet("/CategorySearchServlet")
public class CategorySearchServlet extends HttpServlet {
	private static final String STRING_FOOTER = "<footer><a href='page1.html'>Back to previous page</a></footer>";
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CategorySearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if(session==null)
			return;
		session.setAttribute("categorySearch", request.getParameter("category"));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		String destination = "/createHTML.jsp";

		if(session==null){
			request.setAttribute("text",ResponseHTML.pageWithNoSessionError());
			RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
			return;
		}
		String category = (String) session.getAttribute("categorySearch");
		List<Category>categoryList = DataBaseController.getTheLowestSubcategoiresOfCategory(category);
		
		if(categoryList == null){
			request.setAttribute("text",ResponseHTML.pageChooseCategory());
			RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
			return ;
		}
		Iterator it = categoryList.iterator();
		
			StringBuffer buff = new StringBuffer();
			while(it.hasNext()){
			Category c = (Category) it.next();
			List<Product> products = DataBaseController.getProductFromDB(c.getId());
			String nameProductFilter = request.getParameter("whatToSearchFor");
			ProductFilter p = new ProductFilter(nameProductFilter);

			if(products.size() > 0){
			 if(nameProductFilter.isEmpty()){
                 buff.append(ResponseHTML.createHTMLFromProductList(products,null,"true"));}
			 else {
				 buff.append(ResponseHTML.createHTMLFromProductList(products,p,"true"));}
				}			
			}
			buff.append(STRING_FOOTER);
			
				request.setAttribute("text",buff.toString());
				RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
				rd.forward(request, response);
				return;
			
			}
		
	}
